package org.lx.nursing.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.lx.common.core.controller.BaseController;
import org.lx.common.core.domain.AjaxResult;
import org.lx.common.core.page.TableDataInfo;
import org.lx.nursing.domain.Reservation;
import org.lx.nursing.dto.ReservationDto;
import org.lx.nursing.dto.ReservationPageDto;
import org.lx.nursing.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member/reservation")
@Api(tags = "C端-预约相关接口")
@Slf4j
public class ReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    /**
     * 查询当天取消预约数量
     * - 接口地址:/member/reservation/cancelled-count
     * - 请求方式:GET
     */
    @RequestMapping("/cancelled-count")
    public AjaxResult getCancelledCount() {
        Integer count = reservationService.getCancelledCount();
        return AjaxResult.success(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     * - 接口地址:/member/reservation/countByTime
     * - 请求方式:GET
     */
    @RequestMapping("/countByTime")
    public AjaxResult getCountByTime(Long time) {
        List<Map<String, Object>> countList =reservationService.getCountByTime(time);
        return AjaxResult.success(countList);
    }

    /**
     *新增预约
     * - 接口地址:/member/reservation/
     * - 请求方式:POST
     */
    @PostMapping
    public AjaxResult addReservation(@RequestBody Reservation reservation){
        log.info("-----新增预约信息:{}",reservation);
        boolean result = reservationService.insertReservation(reservation);
        if (result){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询预约
     * - 接口地址:/member/reservation/page
     * - 请求方式:GET
     */
    @GetMapping("/page")
    public AjaxResult getReservationPage(ReservationPageDto reservationDto){
        log.info("-----分页查询预约信息:{}",reservationDto);
        List<Reservation> list = reservationService.getReservationPage(reservationDto);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     *  取消预约
     * - 接口地址:/member/reservation/{id}/cancel
     * - 请求方式:PUT
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id){
        boolean result = reservationService.cancelReservation(id);
        if (result){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}
